from pydantic import BaseModel, Field
from typing import Optional, List, Any
from datetime import datetime
from models import RoleType

# 通用响应模型
class ResponseModel(BaseModel):
    code: int = 200
    message: str = "success"
    data: Optional[Any] = None

# 用户相关模型
class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    real_name: str = Field(..., min_length=1, max_length=100, description="真实姓名")
    phone: Optional[str] = Field(None, max_length=20, description="电话号码")
    student_id: Optional[str] = Field(None, max_length=20, description="学号")
    contact_info: Optional[str] = Field(None, description="联系信息")
    role_type: RoleType = Field(RoleType.READER, description="用户角色")

class UserCreate(UserBase):
    password: str = Field(..., min_length=6, max_length=50, description="密码")
    confirm_password: str = Field(..., min_length=6, max_length=50, description="确认密码")

class UserLogin(BaseModel):
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")

class UserResponse(UserBase):
    id: int
    is_active: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

# Token相关模型
class TokenData(BaseModel):
    username: Optional[str] = None

class TokenResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user_info: UserResponse

# 图书相关模型
class BookBase(BaseModel):
    isbn: str = Field(..., min_length=10, max_length=20, description="ISBN")
    title: str = Field(..., min_length=1, max_length=200, description="书名")
    author: str = Field(..., min_length=1, max_length=100, description="作者")
    publisher: Optional[str] = Field(None, max_length=100, description="出版社")
    publish_date: Optional[str] = Field(None, max_length=20, description="出版日期")
    category: Optional[str] = Field(None, max_length=50, description="分类")
    keywords: Optional[str] = Field(None, description="关键词")
    description: Optional[str] = Field(None, description="描述")
    stock: int = Field(1, ge=0, description="库存数量")

class BookCreate(BookBase):
    pass

class BookResponse(BookBase):
    id: int
    available_stock: int
    is_active: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

class BookListResponse(BaseModel):
    books: List[BookResponse]
    total: int
    page: int
    page_size: int
    total_pages: int

# 分页参数
class PaginationParams(BaseModel):
    page: int = Field(1, ge=1, description="页码")
    page_size: int = Field(10, ge=1, le=100, description="每页数量")
    keyword: Optional[str] = Field(None, description="搜索关键词")